package org.lboot.rabbit.test;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.lboot.rabbit.client.RocketRabbitClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mq")
@AllArgsConstructor
public class MqTestController {
    RocketRabbitClient client;

    @SneakyThrows
    @GetMapping("simple")
    public String simpleTest(){
        String QUEUE_NAME = "hello";
        return client.publishMessage(QUEUE_NAME,"xx1");
    }

    @ApiOperation(value = "简单消费1")
    @PostMapping("customer1")
    @SneakyThrows
    public void simpleCustomer(){
        Channel channel = client.getChannel();
        String QUEUE_NAME = "hello";
        // 消费消息的回调
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("成功消费消息,内容为:" + new String(message.getBody()));
        };
        // 取消消费的回调
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息消费被中断");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
    }
}
